package cn.edu.ecut.se2024.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.edu.ecut.se2024.exception.OperationFailedException;
import cn.edu.ecut.se2024.model.supplier_mgmt.Supplier;
import cn.edu.ecut.se2024.service.SupplierMgmtService;
import jakarta.servlet.http.HttpServletRequest;

@Controller
public class SupplierMgmtController extends AuthorizedController {
	public static final String SUPPLIER_MGMT_SUPPLIERLIST = "SupplierMgmtSupplierList";
	public static final String SUPPLIER_MGMT_SUPPLIER = "SupplierMgmtSupplier";
	public static final String SUPPLIER_MGMT_SUPPLIER_NAME = "SupplierMgmtSupplierName";
	public static final String SUPPLIER_MGMT_POWER = "SupplierMgmtPower";
	public static final String SUPPLIER_MGMT_POWER_MGMT = "SupplierMgmtPowerMgmt";
	public static final String SUPPLIER_MGMT_POWER_SEE = "SupplierMgmtPowerSee";
	public static final String SUPPLIER_MGMT_ACTION = "SupplierMgmtAction";
	public static final String SUPPLIER_MGMT_ACTION_SEE = "SupplierMgmtActionSee";
	public static final String SUPPLIER_MGMT_ACTION_ADD = "SupplierMgmtActionAdd";
	public static final String SUPPLIER_MGMT_ACTION_UPDATE = "SupplierMgmtActionUpdate";
	public static final String SUPPLIER_MGMT_TIP = "SupplierMgmtTip";

	@Autowired
	private SupplierMgmtService supplierMgmtService;
	
	@RequestMapping("/suppliermgmt.mgmt")
	public String managementAllSupplier(HttpServletRequest request) {
		// 保存用户管理权限
		request.getSession().setAttribute(SupplierMgmtController.SUPPLIER_MGMT_POWER, SupplierMgmtController.SUPPLIER_MGMT_POWER_MGMT);
		// 获取并保存所有供应商信息
		ArrayList<Supplier> supplierlist = supplierMgmtService.getAllSupplierInformation();
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_SUPPLIERLIST, supplierlist);
		
		return SYS_MAIN_PAGE;
	}
	
	@RequestMapping("/suppliermgmt.see")
	public String seeAllSupplier(HttpServletRequest request) {
		// 保存用户查看权限
		request.getSession().setAttribute(SupplierMgmtController.SUPPLIER_MGMT_POWER, SupplierMgmtController.SUPPLIER_MGMT_POWER_SEE);
		// 获取并保存所有供应商信息
		ArrayList<Supplier> supplierlist = supplierMgmtService.getAllSupplierInformation();
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_SUPPLIERLIST, supplierlist);

		return SYS_MAIN_PAGE;
	}
	
	@PostMapping("/suppliermgmt.searchSupplier")
	public String searchSupplierName(HttpServletRequest request, @RequestParam @Validated String supplier_name) {
		// 保存supplier_name
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_SUPPLIER_NAME, supplier_name);
		// 获取名称为supplier_name的所有供应商信息并保存
		ArrayList<Supplier> supplierlist = supplierMgmtService.getSupplierNameInformation(supplier_name);
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_SUPPLIERLIST, supplierlist);

		return SYS_MAIN_PAGE;
	}
	
	@PostMapping("/suppliermgmt.seeSupplier")
	public String seeSupplier(HttpServletRequest request, @RequestParam @Validated int supplier_id) throws OperationFailedException {
		// 保存用户操作动作（查看）
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_ACTION, SupplierMgmtController.SUPPLIER_MGMT_ACTION_SEE);
		// 获取ID为supplier_id的供应商信息并保存
		Supplier supplier = supplierMgmtService.getSpecialSupplierInformation(supplier_id);
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_SUPPLIER, supplier);
		
		return SYS_MAIN_PAGE;
	}
	
	@RequestMapping("/suppliermgmt.toAddSupplier")
	public String toAddSupplier(HttpServletRequest request) {
		// 保存用户操作动作（增加）
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_ACTION, SupplierMgmtController.SUPPLIER_MGMT_ACTION_ADD);

		return SYS_MAIN_PAGE;
	}
	
	@PostMapping("/suppliermgmt.addSupplier")
	public String addSupplier(HttpServletRequest request, @RequestParam @Validated String supplier_json) throws OperationFailedException {
		// 解析JSON字符串，将其映射为Supplier对象
		ObjectMapper mapper = new ObjectMapper();
		Supplier supplier = null;
		try {
			supplier = mapper.readValue(supplier_json, Supplier.class);
		} catch (Exception e) {
			throw new OperationFailedException("解析数据失败！");
		}
		
		// 增加供应商信息
		boolean flag = supplierMgmtService.addSupplierInformation(supplier);
		// 设置提示信息
		if (flag){
			request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_TIP, "成功增加了一条ID为" + supplier.getId() + "的供应商信息！");
		}else {
			request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_TIP, "增加供应商信息失败，请稍后重试！");
		}
		
		return SYS_MAIN_PAGE;
	}
	
	@PostMapping("/suppliermgmt.toUpdateSupplier")
	public String toUpdateSupplier(HttpServletRequest request, @RequestParam @Validated int supplier_id) throws OperationFailedException {
		// 保存用户操作动作（修改）
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_ACTION, SupplierMgmtController.SUPPLIER_MGMT_ACTION_UPDATE);
		// 获取ID为supplier_id的供应商信息并保存
		Supplier supplier = supplierMgmtService.getSpecialSupplierInformation(supplier_id);
		request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_SUPPLIER, supplier);
		
		return SYS_MAIN_PAGE;
	}
	
	@PostMapping("/suppliermgmt.updateSupplier")
	public String updateSupplier(HttpServletRequest request, @RequestParam @Validated String supplier_json) throws OperationFailedException {
		// 解析JSON字符串，将其映射为Supplier对象
		ObjectMapper mapper = new ObjectMapper();
		Supplier supplier = null;
		try {
			supplier = mapper.readValue(supplier_json, Supplier.class);
		} catch (Exception e) {
			throw new OperationFailedException("解析数据失败！");
		}
		
		// 修改供应商信息
		boolean flag = supplierMgmtService.updateSupplierInformation(supplier);
		// 设置提示信息
		if (flag){
			request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_TIP, "成功修改了一条ID为" + supplier.getId() + "的供应商信息！");
		}else {
			request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_TIP, "修改供应商信息失败，请稍后重试！");
		}
		
		return SYS_MAIN_PAGE;
	}
	
	@PostMapping("/suppliermgmt.deleteSupplier")
	public String deleteSupplier(HttpServletRequest request, @RequestParam @Validated int supplier_id) throws OperationFailedException {
		// 删除供应商信息
		boolean flag = supplierMgmtService.deleteSupplierInformation(supplier_id);
		// 设置提示信息
		if (flag){
			request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_TIP, "成功删除了一条ID为" + supplier_id + "的供应商信息！");
		}else {
			request.setAttribute(SupplierMgmtController.SUPPLIER_MGMT_TIP, "删除供应商信息失败，请稍后重试！");
		}
		
		return SYS_MAIN_PAGE;
	}
}
